package com.ls.customer.service;

import com.ls.customer.entity.CustomerAppInter;
import com.ls.customer.entity.InterOrder;
import com.ls.customer.entity.InterPrice;
import com.ls.customer.repository.CustomerApptinerRepository;
import com.ls.customer.repository.InterPriceRepository;
import com.ls.customer.repository.InterRepository;
import com.ls.customer.entity.Inter;
import com.ls.customer.repository.InterorderRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 钰杰
 * @className InterService
 * @description VX:FengYJ996
 * @date 2020/1/29:03
 */
@Service
public class InterService {

    @Autowired
    private InterRepository interRepository;

    @Autowired
    private InterPriceRepository interPriceRepository;

    @Autowired
    private InterorderRespository interorderRespository;

    @Autowired
    private CustomerApptinerRepository customerApptinerRepository;

    //获取所有接口
    public List<Inter> getIntersAll(){
        return interRepository.findAll();
    }

    //根据id获取接口
    public Inter getInterByid(Integer interid){
        return interRepository.findById(interid).get();
    }

    //根据id获取价格明细
    public InterPrice getInterPriceByInterfaceid(String interfaceid) {
        return interPriceRepository.findByInterfaceId(Integer.parseInt(interfaceid));
    }

    //生成订单
    @Transactional
    public InterOrder addInterOrder(InterOrder interOrder) {
        interOrder.setStatus("1");//默认未支付
        interOrder.setCdate(new Date());
        InterOrder save = interorderRespository.save(interOrder);
        return save;
    }

    //获取订单信息
    public InterOrder getinterOrder(Integer interoid) {
       return interorderRespository.findById(interoid).get();
    }

    //修改订单状态
    @Transactional
    public void updInterOrderStatus(String status , Integer interoid) {
        interorderRespository.updInterOrderStatus(status,interoid);
    }

    //申请接口
    @Transactional
    public void applyInter(String appid, String interid) {
        CustomerAppInter customerAppInter = new CustomerAppInter();
        customerAppInter.setAppid(appid);
        customerAppInter.setDstatus(1);
        customerAppInter.setInterfaceId(Integer.parseInt(interid));
        customerAppInter.setStatus(0);
        customerApptinerRepository.save(customerAppInter);
    }



}
